package com.cqupt.scanning.system.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.DeviceFoot;
import com.cqupt.scanning.system.admin.entity.DeviceThree;
import com.cqupt.scanning.system.admin.entity.PatientFoot;
import com.cqupt.scanning.system.admin.entity.PatientThree;
import com.cqupt.scanning.system.admin.entity.dto.PatientFootDTO;
import com.cqupt.scanning.system.admin.mapper.PatientThreeMapper;
import com.cqupt.scanning.system.admin.service.DeviceThreeService;
import com.cqupt.scanning.system.admin.service.PatientThreeService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 病人用户（前端展示） 服务实现类
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-06
 */
@Service
public class PatientThreeServiceImpl extends ServiceImpl<PatientThreeMapper, PatientThree> implements PatientThreeService {
    @Autowired
    private AclUserService aclUserService;
    @Autowired
    private DeviceThreeService deviceThreeService;

    @Override
    public R getAll(Page<PatientThree> page, String name,String ageLower,String ageUpper,String doctor,String heightLower,String heightUpper,String measureMode,String sex,String weightLower,String weightUpper,String beginTime,String endTime,String userName,String phone,String orgName,String devNum,
                    String businessName,String address,String cityProvince,String country,String fax, String weightBearingLower, String weightBearingUpper,String dressShoe,String athleticShoe,String casualShoe,String heels,String age) {
        QueryWrapper<PatientThree> w = new QueryWrapper<>();
        if (!StrUtil.isEmpty(name)){
            w.like("name",name);
        }
        if (!StrUtil.isEmpty(age)){
            w.like("age",age);
        }
        if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
            w.ge("age", ageLower).le("age", ageUpper);
        }
        if (!StrUtil.isEmpty(doctor)){
            w.like("doctor",doctor);
        }
        if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
            w.ge("height",heightLower).le("height",heightUpper);
        }
        if (!StrUtil.isEmpty(measureMode)){
            w.like("gaitMeasureMode",measureMode);
        }
        if (!StrUtil.isEmpty(sex)){
            w.eq("sex", sex);
        }
        if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
            w.ge("weight",weightLower).le("weight",weightUpper);
        }
        if (!StrUtil.isEmpty(orgName)){
            w.like("org_name",orgName);
        }
        if (!StrUtil.isEmpty(devNum)){
            w.like("dev_num",devNum);
        }
        if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
            w.ge("create_time",beginTime).le("create_time",endTime);
        }
        if(!StrUtil.isEmpty(phone)){
            w.eq("phone",phone);
        }
        if(!StrUtil.isEmpty(businessName)){
            w.like("business_name",businessName);
        }
        if(!StrUtil.isEmpty(address)){
            w.like("address",address);
        }
        if(!StrUtil.isEmpty(cityProvince)){
            w.like("city_province",cityProvince);
        }
        if(!StrUtil.isEmpty(country)){
            w.like("country",country);
        }
        if(!StrUtil.isEmpty(fax)){
            w.like("fax",fax);
        }
        if (!StrUtil.isEmpty(weightBearingLower) && !StrUtil.isEmpty(weightBearingUpper)){
            w.ge("weight_bearing",weightBearingLower).le("weight_bearing",weightBearingUpper);
        }
        if(!StrUtil.isEmpty(dressShoe)){
            w.like("dress_shoe",dressShoe);
        }
        if(!StrUtil.isEmpty(athleticShoe)){
            w.like("athletic_shoe",athleticShoe);
        }
        if(!StrUtil.isEmpty(casualShoe)){
            w.like("casual_shoe",casualShoe);
        }
        if(!StrUtil.isEmpty(heels)){
            w.like("heels",heels);
        }
        if (!StrUtil.isEmpty(userName)){
            AclUser user = aclUserService.getOne(new QueryWrapper<AclUser>().eq("note",0).eq("username", userName).select("id"));
            if(user==null){
                return R.error().message("未查询到"+userName+"的相关数据");
            }
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().eq("user_id", user.getId()).select("dev_num"));
            w.like("dev_num",deviceThree.getDevNum());
        }
        IPage<PatientThree> patientThreeIPage = baseMapper.selectPage(page, w); //根据 w(可为null)的条件，查询全部记录（并翻页）
        List<PatientThree> records = patientThreeIPage.getRecords();
        List<PatientThree> patientThrees = new ArrayList<>();
        for (PatientThree record : records) {
            PatientThree patientThree = new PatientThree();
            BeanUtils.copyProperties(record, patientThree);
            patientThrees.add(patientThree);
        }
        System.out.println("第"+patientThreeIPage.getCurrent()+"页，本次查询共有条数为"+patientThreeIPage.getTotal());
        return R.ok().data("patients", patientThreeIPage).data("total", patientThreeIPage.getTotal());

    }
}
